package com.i_art_engineer.meigong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.adapter.GuideViewAdapter;
import com.i_art_engineer.meigong.base.FullScreenActivity;
import com.i_art_engineer.meigong.base.ImmersiveActivity;

import java.util.ArrayList;

/**
 *
 */
public class GuideActivity extends FullScreenActivity implements OnClickListener, OnPageChangeListener {

    //定义ViewPager对象
    private ViewPager viewPager;

    //定义一个ArrayList来存放View
    private ArrayList<View> views;

    private GuideViewAdapter adapter;

    //引导图片资源
    private static final int[] pics = {R.drawable.guide1, R.drawable.guide2, R.drawable.guide3};

    //底部小点的图片
    private ImageView[] dots;

    //记录当前选中位置
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        initViews();

        //去掉引导页下面的点
        initDots();
    }

    private void initViews() {
        views = new ArrayList<>();

        //定义一个布局并设置参数
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );

        //初始化引导图片列表
        for (int i = 0; i < pics.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setAdjustViewBounds(true);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(mParams);
            iv.setImageResource(pics[i]);
            views.add(iv);
        }

        adapter = new GuideViewAdapter(views, this);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);
    }

    private void initDots() {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.dots);
        dots = new ImageView[views.size()];
        for (int i = 0; i < views.size(); i++) {
            dots[i] = (ImageView) linearLayout.getChildAt(i);
            //默认都设为黑色
            dots[i].setEnabled(true);
            //给每个小点设置监听
            dots[i].setOnClickListener(this);
            //设置位置tag，方便取出与当前位置对应
            dots[i].setTag(i);
        }
        currentIndex = 0;
        dots[currentIndex].setEnabled(false);
    }

    private void setCurrentDots(int position) {
        if (position < 0 || position > views.size()) {
            return;
        }
        dots[position].setEnabled(false);
        dots[currentIndex].setEnabled(true);
        currentIndex = position;
    }

    /**
     * 当滑动状态改变时调用
     */
    @Override
    public void onPageScrollStateChanged(int arg0) {
        if(currentIndex== dots.length-1&&arg0==1){
            startActivity(new Intent(GuideActivity.this, LoginActivity.class));
            finish();
        }
    }

    /**
     * 当前页面被滑动时调用
     */

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    /**
     * 当新的页面被选中时调用
     */

    @Override
    public void onPageSelected(int position) {
        //设置底部小点选中状态
        setCurDot(position);
    }

    /**
     * 通过点击事件来切换当前的页面
     */
    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag();
        setCurView(position);
        setCurDot(position);
    }

    /**
     * 设置当前页面的位置
     */
    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }
        viewPager.setCurrentItem(position);
    }

    /**
     * 设置当前的小点的位置
     */
    private void setCurDot(int positon) {
        if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
            return;
        }
        dots[positon].setEnabled(false);
        dots[currentIndex].setEnabled(true);

        currentIndex = positon;
    }

}
